/*
 * UniformGenerator.java
 *
 * Created on Piatok, 2007, november 2, 20:28
 *
 */

package logic.distributions;

import eduni.simjava.distributions.Sim_uniform_obj;
import java.util.Random;
import java.util.Vector;

/**
 * Generator rovnomerneho rozdelenia
 * Parameter 0 - minimum -> Double, zadavany ako String, rozsah (0,maximum)
 * Parameter 1 - maximum -> Double, zadavany ako String, rozsah (minimum, nekonecno)
 * @author alvin
 */
public class UniformGenerator extends AbstractGenerator{
    private Sim_uniform_obj generator = null;
            
    protected Vector<String> generateParameterNames() {
        Vector<String> result = new Vector<String>(2);
        result.add("Minimum");
        result.add("Maximum");
        return result;
    }

    public void initValues() {
        try {
            setUncheckedParameter(0, "10");
            setUncheckedParameter(1, "20");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private double getMax() {
        return Double.parseDouble((String)getParameter(1));
    }

    private double getMin() {
        return Double.parseDouble((String)getParameter(0));
    }
    
    public String checkParameter(int index, Object value) {
        if (!(value instanceof String))
            return "Bad type";
        
        String s = (String)value;
        
        try {
            double d = Double.parseDouble(s);
            if (d < 0.0)
                return "Value must be > 0.0";
            if ((index == 0) && (d > getMax()))
                return "Minimum must be < " + getMax();
            if ((index == 1) && (d < getMin()))
                return "Maximum must be > " + getMin();
        
        }
        catch (NumberFormatException e) {
            return "Not valid float value " + s;
        }
        
        return null;
    }

    public void initGenerator(String generatorName) {
        Random rg = new Random();
        generator = new Sim_uniform_obj(generatorName, getMin(), getMax(), rg.nextLong());
    }

    public boolean isDiscreteGenerator() {
        return false;
    }

    public long getDiscreteValue() {
        return Math.round(getContinuousValue());
    }

    public double getContinuousValue() {
        return generator.sample();
    }

    public String getTypeString() {
        return "UniformGenerator";
    }
    
}
